package com.demo.forum.services.impl;

import com.demo.forum.common.AppResult;
import com.demo.forum.common.ResultCode;
import com.demo.forum.dao.MessageMapper;
import com.demo.forum.exception.ApplicationException;
import com.demo.forum.model.Message;
import com.demo.forum.services.IMessageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 站内信相关的业务实现
 */
// 日志注解
@Slf4j
// 加入到Spring容器
@Service
public class MessageServiceImpl implements IMessageService {
    // 注入DAO
    @Resource
    private MessageMapper messageMapper;
    @Override
    public void create(Message message) {
        // 非空校验
        if (message == null || message.getReceiveUserId() == null) {
            // 记录日志
            log.info(ResultCode.ERROR_IS_NULL.toString());
            // 抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_IS_NULL));
        }

        // 填充默认值
        message.setState((byte) 0); // 未读状态
        message.setDeleteState((byte) 0); // 是否删除
        Date date = new Date();
        message.setCreateTime(date);
        message.setUpdateTime(date);
        // 定入数据库
        int row = messageMapper.insertSelective(message);
        if (row != 1) {
            // 记录日志
            log.info(ResultCode.FAILED_CREATE.toString() + "messageId = " + message.getId());
            // 抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_CREATE));
        }
        // 记录日志, 记录一下返回Id
        log.info("发送站内信成功：postUserId = " + message.getPostUserId() + ", receiveUserId = " + message.getReceiveUserId());

    }

    @Override
    public Message selectById(Long id) {
        // 非空校验
        if (id == null) {
            // 记录日志
            log.info(ResultCode.ERROR_IS_NULL.toString());
            // 抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_IS_NULL));
        }
        // 查询结果
        Message result = messageMapper.selectByPrimaryKey(id);
        // 返回结果
        return result;
    }

    @Override
    public Integer selectUnreadCount(Long userId) {
        // 非空校验
        if (userId == null) {
            // 记录日志
            log.info(ResultCode.ERROR_IS_NULL.toString());
            // 抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_IS_NULL));
        }
        // 查询结果
        Integer result = messageMapper.selectUnreadCount(userId);
        // 返回结果
        return result;
    }

    @Override
    public List<Message> selectPage(Long userId, Integer page, Integer num, String order, boolean isAsc) {
        return null;
    }

    @Override
    public void updateById(Message message) {
        // 非空校验
        if (message == null || message.getId() == null) {
            // 记录日志
            log.info(ResultCode.ERROR_IS_NULL.toString());
            // 抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_IS_NULL));
        }
        // 更新
        int row = messageMapper.updateByPrimaryKeySelective(message);
        if (row != 1) {
            // 记录日志
            log.info(ResultCode.FAILED.toString() + "messageId = " + message.getId());
            // 抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED));
        }
    }
}
